package com.wswy.wzcx.model.home;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.wswy.wzcx.AppContext;
import com.wswy.wzcx.api.ConfigsKt;
import com.wswy.wzcx.model.MessageMode;
import com.wswy.wzcx.ui.Constants;
import com.wswy.wzcx.ui.TestActivity;
import com.wswy.wzcx.ui.activity.MineActivity;
import com.wswy.wzcx.ui.activity.ModuleActivity;
import com.wswy.wzcx.ui.activity.ModuleHolderActivity;
import com.wswy.wzcx.ui.activity.news.ResultNewsActivity;
import com.wswy.wzcx.ui.car.AddCarPreActivity;
import com.wswy.wzcx.ui.car.remind.RemindActivity;
import com.wswy.wzcx.ui.car.result.QueryResultActivity;
import com.wswy.wzcx.ui.coupon.CouponListFragment;
import com.wswy.wzcx.ui.dmv.CarDepartmentActivity;
import com.wswy.wzcx.ui.fkdj.FinePaymentActivity;
import com.wswy.wzcx.ui.fragment.BookKeepingFragment;
import com.wswy.wzcx.ui.fragment.JZCFFragment;
import com.wswy.wzcx.ui.fragment.MyCarCollectFragment;
import com.wswy.wzcx.ui.fragment.MyOrderFragment;
import com.wswy.wzcx.ui.fragment.WZDBFragment;
import com.wswy.wzcx.ui.fragment.WyServiceFragment;
import com.wswy.wzcx.ui.main.community.CommunityFragment;
import com.wswy.wzcx.ui.main.community.detail.TopicDetailActivity;
import com.wswy.wzcx.ui.message.MessageDetailFragment;
import com.wswy.wzcx.ui.message.MessageFragment;
import com.wswy.wzcx.ui.user.LoginHolderActivity;
import com.wswy.wzcx.ui.weather.WeatherActivity;
import com.wswy.wzcx.ui.web.WebPageArgument;
import com.wswy.wzcx.ui.web.WebViewActivity;
import com.wswy.wzcx.ui.wzdb.WzdbPickFragment;
import com.wswy.wzcx.utils.ShareHelper;

import java.util.HashMap;
import java.util.Map;

public class RouterUtils {

  /**
   * 原生页面
   */
  public static final int TYPE_NATIVE = 0;
  /**
   * 网页
   */
  public static final int TYPE_WEB = 1;
  /**
   * 推啊广告入口
   */
  public static final int TYPE_TUIA_AD = 2;

  /**
   * 拉起微信小程序
   */
  public static final int TYPE_WX_MINI_PROGRAM = 3;

  private static Map<String, Intent> sRouterMap = new HashMap<>();

  public static boolean isWeb(int type){
    return type == TYPE_WEB;
  }

  public static boolean isNative(int type){
    return type == TYPE_NATIVE;
  }

  private static void init(Context ctx){
    if(sRouterMap.isEmpty()){
      Context context = ctx.getApplicationContext();
      sRouterMap.put(RPaths.login,new Intent(context,LoginHolderActivity.class)); //登录

      sRouterMap.put(RPaths.cgs,new Intent(context, CarDepartmentActivity.class));//车管所
      sRouterMap.put(RPaths.fkdj,new Intent(context,FinePaymentActivity.class));//罚款代缴
      sRouterMap.put(RPaths.jzcf, ModuleActivity.getTargetIntent(context, JZCFFragment.TAG));//驾照查分

      sRouterMap.put(RPaths.wzdb, ModuleActivity.getTargetIntent(context, WZDBFragment.TAG));//违章代办
      sRouterMap.put(RPaths.wzdbChooseCar, ModuleActivity.getTargetIntent(context, WzdbPickFragment.TAG));//违章代办选择车辆

      sRouterMap.put(RPaths.order, MineActivity.asIntent(context, MyOrderFragment.TAG)); //我的订单
      sRouterMap.put(RPaths.message, MineActivity.asIntent(context, MessageFragment.TAG)); //消息

      sRouterMap.put(RPaths.feedback, MineActivity.asIntent(context, RPaths.feedback)); //意见反馈

      sRouterMap.put(RPaths.my_cars, MineActivity.asIntent(context, MyCarCollectFragment.TAG));
      sRouterMap.put(RPaths.add_car,new Intent(context,AddCarPreActivity.class)); //添加车辆
      sRouterMap.put("_test",new Intent(context,TestActivity.class)); //测试


      sRouterMap.put(RPaths.njtx,RemindActivity.Companion.getInspectionIntent(context)); //年检
      sRouterMap.put(RPaths.bxtx,RemindActivity.Companion.getInsuranceIntent(context)); //保险

      sRouterMap.put(RPaths.yczb, MineActivity.asIntent(context, BookKeepingFragment.TAG)); //养车账本

      sRouterMap.put(RPaths.carQuery,new Intent(context, QueryResultActivity.class)); //车辆违章

      sRouterMap.put(RPaths.customer_service, MineActivity.asIntent(context, WyServiceFragment.TAG)); //在线客服

      sRouterMap.put(RPaths.news,new Intent(context,ResultNewsActivity.class));

      sRouterMap.put(RPaths.topic_info,new Intent(context,TopicDetailActivity.class));

      //社交消息列表
      sRouterMap.put(RPaths.social_msg_list, MineActivity.asIntent(context, MessageDetailFragment.TAG, MessageMode.MESSAGE_SOCIAL));

      sRouterMap.put(RPaths.weather,new Intent(context,WeatherActivity.class));

      sRouterMap.put(RPaths.bbs, ModuleActivity.getTargetIntent(context, CommunityFragment.TAG,true)); //吐槽

      sRouterMap.put(RPaths.mycoupon, ModuleActivity.getTargetIntent(context, CouponListFragment.TAG)); //我的优惠券


      //注意缓存数据！
      sRouterMap.put(RPaths.web12123_faq,getWebIntent(context,ConfigsKt.H5_TAB_12123())); //交管12123常见问题
      sRouterMap.put(RPaths.contact,getWebIntent(context,ConfigsKt.H5_CUSTOMER_SERVICE())); //联系客服
      sRouterMap.put(RPaths.share_app,new Intent()); //介绍给朋友
      sRouterMap.put(RPaths.gotoMarket,new Intent());
      sRouterMap.put(RPaths.tuia_ad,new Intent());

      addPage(context, RPaths.OIL_PRICES);  //今日油价
      addPage(context, RPaths.OIL_TREND);  //油价趋势
      addPage(context, RPaths.LBS_CAR_PARK);  //附近停车场
      addPage(context, RPaths.LBS_GAS_STATION); //附近加油站
      addPage(context, RPaths.LBS_CAR_WASH);  //附近洗车场
      addPage(context, RPaths.LBS_CAR_4S); //附近4s店

      addPage(context, RPaths.COMMON_NUMBERS);  //应急电话
      addPage(context, RPaths.ALL_MODULES);  //全部应用

      addPage(context, RPaths.ADD_FEEDBACK); //添加反馈
      addPage(context, RPaths.MY_FEEDBACK); //我的反馈
      addPage(context, RPaths.HISTORY_FINE); //历史违章


      addPage(context, RPaths.MY_LICENSE); //我的证件
      addPage(context, RPaths.NJDB); //年检代办

      addPage(context, RPaths.KF_WECHAT); //联系客服

      addPage(context, RPaths.SETTINGS_PRIVACY);
      //sRouterMap.put(RPaths.ALL_MODULES,new Intent(context, KPreWebActivity.class));
    }
  }

  private static void addPage(Context context,String path){
    sRouterMap.put(path, ModuleActivity.getTargetIntent(context,path));
  }

  private static void addHolderPage(Context context,String path){
    sRouterMap.put(path, ModuleHolderActivity.getTargetIntent(context,path));
  }

  public static boolean hasPage(Context context,String targetUrl){
    init(context);

    return sRouterMap.containsKey(targetUrl);
  }

  public static @Nullable Intent getTargetIntent(Context context,String targetUrl){
    init(context);
    return sRouterMap.get(targetUrl);
  }

  public static @NonNull Intent getWebIntent(Context context,String targetUrl){
    Intent intent = new Intent(context, WebViewActivity.class);
    //intent.putExtra("url",targetUrl);
    intent.putExtra(Constants.EXTRA_DATA,new WebPageArgument(targetUrl));
    return intent;
  }

  public static void  startWeb(Context context, WebPageArgument webPageArgument){
    Intent intent = new Intent(context, WebViewActivity.class);
    intent.putExtra(Constants.EXTRA_DATA,webPageArgument);
    context.startActivity(intent);
  }

  public static void startWeb(Context context,String url){
    context.startActivity(getWebIntent(context, url));
  }

  public static @NonNull Intent getLogin(){
    Intent intent = sRouterMap.get(RPaths.login);
    if (intent == null){
      intent = new Intent(AppContext.getContext(),LoginHolderActivity.class);
      sRouterMap.put(RPaths.login,intent);
    }
    return intent;
  }

  public static Intent getIntent(Context context,int type,String targetUrl){
    if(isWeb(type)){

      try {
        //http协议自己打开，其它情况下使用系统打开
        if (targetUrl.startsWith("http://") || targetUrl.startsWith("https://")){
          return getWebIntent(context, targetUrl);
        }else {
          Intent intent = new Intent(Intent.ACTION_VIEW);
          intent.setData(Uri.parse(targetUrl));
          if (intent.resolveActivity(context.getPackageManager()) != null) {
            return intent;
          }
        }
      }catch (Throwable e){
        e.printStackTrace();
      }
      return null;
    }else if(isNative(type)){

//      if ("web12123_faq".equals(targetUrl)){ //交管12123常见问题
//        return getWebIntent(context,ConfigsKt.H5_TAB_12123);
//      }else if ("contact".equals(targetUrl)){ //联系客服
//        return getWebIntent(context,ConfigsKt.H5_CUSTOMER_SERVICE);
//      }
      return getTargetIntent(context, targetUrl);
    }else {
      Intent intent = getTargetIntent(context, targetUrl);
      if(intent != null){
        return intent;
      }
    }
    return null;
  }


  public static boolean needCarId(String target){
    return  TextUtils.equals(RPaths.bxtx,target) ||
        TextUtils.equals(RPaths.njtx,target) ||
        TextUtils.equals(RPaths.carQuery,target)||
        TextUtils.equals(RPaths.wzdbChooseCar,target);
  }

  public static boolean needTopicId(String target){
    return TextUtils.equals(RPaths.topic_info,target);
  }

  public static void startTarget(Context context,int type,String targetUrl){
    Intent intent = getIntent(context, type, targetUrl);
    if(intent != null){
      context.startActivity(intent);
    }else {
      if (type == TYPE_WX_MINI_PROGRAM){
        //小程序
        startWxMiniProgram(targetUrl);
      }
    }
  }

  public static void start(Context context,String target){
    startTarget(context,TYPE_NATIVE,target);
  }

  public static void start(Context context,String target,Bundle bundle){
    Intent intent1 = getIntent(context, TYPE_NATIVE, target);
    if (intent1 != null){
      if (bundle != null){
        intent1.putExtra(Constants.EXTRA_BUNDLE,bundle);
      }
      context.startActivity(intent1);
    }

  }

  /**
   * 启动微信小程序，
   * @param miniProgram 小程序原始id/二级页面路径，小程序->设置->基本设置最下面，新版本扩展
   */
  public static void startWxMiniProgram(String miniProgram){
    if (!TextUtils.isEmpty(miniProgram)){
      int idx = miniProgram.indexOf('/');
      if (idx == -1){
        //没有耳二级页面
        ShareHelper.startMiniProgram(miniProgram,null);
      }else {
        ShareHelper.startMiniProgram(miniProgram.substring(0,idx),miniProgram.substring(idx+1));
      }
    }

  }

  public static void startAdd(Context context){
    context.startActivity(new Intent(context,AddCarPreActivity.class));
  }

  public static void startPage(Context context,String targetUrl, Bundle bundle){
    context.startActivity(ModuleActivity.getTargetIntent(context,targetUrl,bundle));
  }

}
